$("header").load("header_con.html");
$("article").load("article_list.html");
$("footer").load("footer.html");

//<-------------------获取手机验证码---------------------->
var phone_code = "";
function randomNum(m, n) {
	return Math.floor(Math.random() * (n - m + 1) + m);
}
var timer
$(".get_code").click(function() {
	var phone_code = "";
	var count = 60;
	$(this).val(count + "秒后重发");
	$(this).prop("disabled", true);
	var obj = $(this);
	timer = setInterval(function() {
		count--;
		if(count <= 0) {
			clearInterval(timer);
			obj.val("获取验证码");
			obj.prop("disabled", false);
		}
		obj.val(count + "秒后重发");
	}, 1000)
	for(var i = 0; i < 4; i++) {
		phone_code += randomNum(0, 9);
	}
	setTimeout(function() {
		alert(phone_code);
		$(".user_code").change(function() { 
			if($(this).val() != phone_code) {
				$(this).parent().find("p").html('<img src="img/register/alert.png"/>验证码输入失败');
				bool_flag = false;
			} else {
				$(this).parent().find("p").text("");
				bool_flag = true;
			}
		})
	}, 2000);
});
//提交修改按钮的点击事件
//<--------------------正则表达式匹配-------------------->
var tel_reg = /^1[34578]\d{9}$/;
var pass_reg = /[0-9a-zA-Z]{6,20}/;
var bool_flag = true;
//<--------------------判断手机号码-------------------->
$(".user_phone").change(function() {
	if(!tel_reg.test($(".user_phone").val())) {
		$(".user_phone").parent().next().html('<img src="img/register/alert.png"/>手机号码格式不正确,请再次输入');
		bool_flag = false;
	} else {
		$(".user_phone").parent().next().html('');
		bool_flag = true;
	}
})
//<-------------------判断验证码---------------------->
$(".check_code").change(function() {
	if($(".check_code").val().toLowerCase() == checkCode.toLowerCase()) {
		$(".check_code").parents('.inp_wrap').next().text("");
		bool_flag = true;
	} else {
		bool_flag = false;
		$(".check_code").parents('.inp_wrap').next().html('<img src="img/register/alert.png"/>验证码输入错误');
	}
})
var phone_origin;
var count = 0;
$(".submit_update").click(function() {
	if (bool_flag == true) {
		count++;
		switch (count){
			case 1:
			{
				phone_origin = $(".user_phone").val();
				$(".update_tip").eq(0).attr("src","img/update_phone/check_origion.png");
				$(".update_tip").eq(1).attr("src","img/update_phone/check_new_h.png");
				$(".user_phone").val("");
				$(".check_code").val("");
				$(".user_code").val("");
				$(".get_code").val("获取验证码");
				$(".get_code").prop("disabled",false);
				clearInterval(timer);
				break;
			}
			case 2:
			{
				$.ajax({
					type:"get",
					url:"update_phone.php",
					async:true,
					data:{
						phone_origin:phone_origin,
						phone_new:$(".user_phone").val()
					},
					success:function(data){
						if (data == "1") {
							clearInterval(timer);
							$(".check_origion").css("display","none");
							$(".check_suc").css("display","block");
							deleteCookie("user_phone");
							sessionStorage.user_phone = "";
						}else{
							alert("换绑失败");
							window.location.reload();
						}
					}
				});
				break;
			}
			default:
				break;
		}
	}else{
		alert("请完善信息");
	}
})

//<-----------------------------验证码-------------------------------------->
//随机函数
function randomNum(m, n) {
	return Math.floor(Math.random() * (n - m + 1) + m)
}
//随机颜色
function randomColor() {
	return "rgb(" + randomNum(0, 255) + "," + randomNum(0, 255) + "," + randomNum(0, 255) + ")";
}
var checkCodes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
var checkCode = "";
var code;
var ctx;
//生成验证码
var canvas = document.getElementsByClassName("myCanvas")[0];
function generateCode() {
	//获得cnavas画布
	ctx = canvas.getContext("2d");
	checkCode = "";
	ctx.clearRect(0, 0, canvas.width, canvas.height);
	ctx.beginPath();
	ctx.fillStyle = "#DFF0D8";
	ctx.fillRect(0, 0, canvas.width, canvas.height);
	for (var i = 0; i < 4; i++) {
		code = checkCodes[randomNum(0, checkCodes.length - 1)];
		checkCode += code;
		ctx.save();
		ctx.beginPath();
		ctx.fillStyle = "white";
		ctx.strokeStyle = randomColor();
		ctx.font = "30px serif";
		ctx.rotate(Math.PI / 180 * randomNum(-8, 8));
		ctx.strokeText(code, 10 + i * 15, 25);
		ctx.beginPath();
		ctx.moveTo(randomNum(0, canvas.width), randomNum(0, canvas.height));
		ctx.lineTo(randomNum(0, canvas.width), randomNum(0, canvas.height));
		ctx.stroke();
		ctx.restore();
	}
	return checkCode;
}
generateCode();
//更换验证码
$(".changeCode").click(function() {
	checkCode = "";
	generateCode();
});